package com.umeox.watch.moto.dataservice.proxy;

import android.os.IBinder;
import android.os.RemoteException;

import com.umeox.watch.moto.dataservice.BuildConfig;
import com.umeox.watch.moto.dataservice.interfaces.ILocation;

import net.vidageek.mirror.dsl.Mirror;

public class LocationProxy implements ILocation {
    private IBinder mRemote;

    private LocationProxy(IBinder remote) {
        mRemote = remote;
    }

    public static ILocation fetchBinder() {
        try {
            return new LocationProxy((IBinder) new Mirror().on("android.os.ServiceManager").invoke().method("getService").withArgs(BuildConfig.sosBinderName));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public IBinder asBinder() {
        return mRemote;
    }

    @Override
    public void requestLocation(String reqUser, String locCode, String priority) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(reqUser);
            data.writeString(locCode);
            data.writeString(priority);
            mRemote.transact(TRANSACTION_REQUESTLOCATION, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public byte[] requestLocation4Sos() throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        byte[] result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            mRemote.transact(TRANSACTION_REQUESTLOCATION4SOS, data, reply, 0);
            reply.readException();
            result = reply.createByteArray();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }

}
